package com.tuyou.demo2;

import com.facebook.nifty.client.FramedClientConnector;
import com.facebook.swift.service.ThriftClientManager;
import com.tuyou.demo2.swift.HelloService;
import com.tuyou.demo2.swift.MyException;
import org.apache.thrift.TException;
import java.util.concurrent.ExecutionException;
import static com.google.common.net.HostAndPort.fromParts;
 
/**
 * Created by yangjunming on 3/22/16.
 * author: yangjunming@huijiame.com
 */
public class ThriftClientDemo {
 
    public static void main(String[] args) throws ExecutionException, InterruptedException {
 
        ThriftClientManager clientManager = new ThriftClientManager();
        HelloService helloService = clientManager.createClient(
                new FramedClientConnector(fromParts("localhost", 12345)),
                HelloService.class).get();
        int max = 1;
        Long start = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            try {
                helloService.ping();
            } catch (MyException e) {
                System.out.println(e.getMessage());
            }
        }
        Long end = System.currentTimeMillis();
        Long elapse = end - start;
        int perform = Double.valueOf(max / (elapse / 1000d)).intValue();
 
        System.out.print("thrift " + max + " 次RPC调用，耗时：" + elapse + "毫秒，平均" + perform + "次/秒");

        //mac air：thrift 100000 次RPC调用，耗时：19122毫秒，平均5229次/秒
 
    }
}